package top.ztf.erp.web;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import top.ztf.erp.domain.Permission;
import top.ztf.erp.service.PermissionService;
import top.ztf.erp.service.imp.PermissionServiceImp;
import top.ztf.erp.utils.BeanCopyUtil;
import top.ztf.erp.utils.BeanValidatorUtils;
import top.ztf.erp.utils.ResponseClient;
import top.ztf.erp.utils.TimeUtil;
import top.ztf.erp.vo.PermissionVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

//JSR 303 校验
@WebServlet("/permission")
public class PermissionServlet extends BaseServlet{

    /**
     * 返回菜单树主页
     */
    public void getPermissionTreeIndex(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("WEB-INF/qx/permission/tree.jsp").forward(request,response);
    }
    /**
     * 返回菜单树主页
     */
    public void getPermissionWorkareaIndex(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("WEB-INF/qx/permission/workarea.jsp").forward(request,response);
    }

    /**
     * 获取所有的权限list
     */
    public void getPermissionAll(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        PermissionServiceImp permissionServiceImp = new PermissionServiceImp();
        List<Permission> permissionAll = permissionServiceImp.getPermissionAll();
        ArrayList<PermissionVo> arrayList = new ArrayList<>();
        permissionAll.forEach(t-> arrayList.add(t.clone(PermissionVo.class)));
        String json = JSONObject.toJSONString(arrayList);
        resp.getWriter().print(json);
    }

    /**
     * 根据id 获取权限数据
     */
    public void getPermissionById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        PermissionService permissionServiceImp = new PermissionServiceImp();
        Permission permission = permissionServiceImp.getPermissionById(id);
        req.setAttribute("permission",permission);
        req.getRequestDispatcher("WEB-INF/qx/permission/detail.jsp").forward(req,resp);
    }

    /**
     * 根据父id返回增加权限的视图
     */
    public void getAddPermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pid = req.getParameter("pid");
        req.setAttribute("pid",pid);
        req.getRequestDispatcher("WEB-INF/qx/permission/add.jsp").forward(req,resp);
    }

    /**
     * 增加权限菜单
     */
    public void addPermission(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Permission permission = new Permission();
        BeanUtils.populate(permission,parameterMap);

        Map<String, String> validate = BeanValidatorUtils.validate(permission,Permission.Add.class);
        if (!validate.isEmpty()){
            ResponseClient.fail(JSONObject.toJSONString(validate),resp);
            return;
        }

        permission.setCreateTime(TimeUtil.getNowTime());
        PermissionService permissionService = new PermissionServiceImp();

        int count = permissionService.addPermission(permission);
        ResponseClient.common(count,permission.getId(),resp);
    }

    /**
     * 根据id 获取修改权限菜单视图,
     * 数据回显
     */
    public void getUpdatePermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        PermissionService permissionService = new PermissionServiceImp();
        Permission permission =  permissionService.getPermissionById(id);
        req.setAttribute("permission",permission);
        req.getRequestDispatcher("WEB-INF/qx/permission/edit.jsp").forward(req,resp);
    }

    /**
     * 根据id 修改权限菜单数据
     */
    public void updatePermissionById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Permission permission = new Permission();
        BeanUtils.populate(permission,parameterMap);

        Map<String, String> validate = BeanValidatorUtils.validate(parameterMap, Permission.Update.class);
        if (!validate.isEmpty()){
            ResponseClient.fail(JSONObject.toJSONString(validate),resp);
            return;
        }

        permission.setEditTime(TimeUtil.getNowTime());

        PermissionService permissionService = new PermissionServiceImp();
        int count = permissionService.updatePermissionById(permission);
        ResponseClient.common(count,permission.getId(),resp);
    }

    /**
     * 根据权限菜单id删除 权限菜单
     */
    public void deletePermissionById(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String id = req.getParameter("id");
        PermissionService permissionService = new PermissionServiceImp();
        int count = permissionService.deletePermissionById(id);
        ResponseClient.common(count,resp);
    }

    /**
     * 校验权限菜单名称是否存在
     */
    public void verifyPermissionName(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String name = req.getParameter("name");
        String id = req.getParameter("id");
        PermissionService permissionService = new PermissionServiceImp();
        ResponseClient.common(permissionService.verifyPermissionName(name,id),resp);
    }

}
